package com.example.demo.service;


import com.example.demo.enums.BookStatusEnum;
import com.example.demo.mapper.BookMapper;
import com.example.demo.model.BookInfo;
import com.example.demo.model.PageRequest;
import com.example.demo.model.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BookService {

    @Autowired
    private BookMapper bookMapper;

//    @Autowired
//    private PageRequest pageRequest;

//    @Autowired
//    private BookDao bookDao;
//    public List<BookInfo> getList(){
//        List<BookInfo> bookInfos=bookDao.mockData();
//        for(BookInfo bookInfo:bookInfos){
//            if(bookInfo.getStatus()==1){
//                bookInfo.setStatusCN("可借阅");
//            }else{
//                bookInfo.setStatusCN("不可借阅");
//            }
//        }
//        return bookInfos;
//    }

    public void addBook(BookInfo bookInfo) {
        bookMapper.addBook(bookInfo);
    }

    //@Autowired
    public ResponseResult<BookInfo> getListByPage(PageRequest pageRequest){
        //1.获得总图书
        //2.获得当前页面数据
        Integer count= bookMapper.count();
        List<BookInfo> bookInfos=bookMapper.selectBooksByPage(pageRequest);
        for(BookInfo bookInfo:bookInfos){
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
        return new ResponseResult<>(count,bookInfos,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
       return bookMapper.queryBookById(bookId);
    }

    public void updateBook(BookInfo bookInfo) {
        bookMapper.updateBook(bookInfo);
    }

    public Integer batchDelete(List<Integer> ids){
        return bookMapper.batchDelete(ids);
    }
}
